package com.ngm.tea.tea.service.impl;

import javax.annotation.Resource;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import com.ngm.tea.common.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.ngm.tea.tea.dao.FoodsDao;
import com.ngm.tea.tea.entity.Foods;
import com.ngm.tea.tea.service.FoodsService;

/**
 * 商品实例服务层
 * @author zjw
 * @version 1.0
 * @date 2017-01-03 15:51:34
 *
 */
@Service("foodsService")
public class FoodsServiceImpl implements FoodsService {
	
	@Resource
	private FoodsDao foodsDao;

	public FoodsDao getFoodsDao() {
		return foodsDao;
	}

	@Override
	public void insert(Foods foods) {
		foodsDao.insert(foods);
	}

	@Override
	public void deleteById(Long id) {
		foodsDao.deleteById(id);
	}

	@Override
	public void deleteByIds(String ids) {
		String[] _ids = ids.split(",");
		if (_ids.length > 0) {
			List<String> idlist = Arrays.asList(_ids);
			foodsDao.deleteByIds(idlist);
		}
	}

	@Override
	public void delete(Foods foods) {
		foodsDao.delete(foods);
	}

	@Override
	public void update(Foods foods) {
		foodsDao.update(foods);
	}

	@Override
	public Foods selectOne(Long id) {
		return foodsDao.selectOne(id);
	}

	@Override
	public List<Foods> selectAll() {
		return foodsDao.selectAll();
	}

	@Override
	public List<Foods> selectList(Foods foods, PageInfo pageInfo) {
		Map map = new HashMap<String, Object>();
		map.put("searchParams",makeStr(foods));
		map.put("pageInfo", pageInfo);
		map.put("order", pageInfo.getOrderField()+" "+pageInfo.getOrderDirection());
		return foodsDao.selectList(map);
	}

	@Override
	public Long count(Foods foods) {
		Map map = new HashMap<String, Object>();
		map.put("searchParams",makeStr(foods));
		return foodsDao.count(map);
	}
	
	public String makeStr(Foods foods) {
		StringBuffer sb = new StringBuffer();
		if(!StringUtils.isEmpty(foods.getId())) {
			if (StringUtils.isEmpty(sb.toString())) {
				sb.append("id LIKE '%" + foods.getId()+"%'");
			} else {
				sb.append(" AND id LIKE '%" + foods.getId()+"%'");
			}
		}
		if(!StringUtils.isEmpty(foods.getIcon())) {
			if (StringUtils.isEmpty(sb.toString())) {
				sb.append("icon LIKE '%" + foods.getIcon()+"%'");
			} else {
				sb.append(" AND icon LIKE '%" + foods.getIcon()+"%'");
			}
		}
		if(!StringUtils.isEmpty(foods.getOldPrice())) {
			if (StringUtils.isEmpty(sb.toString())) {
				sb.append("oldPrice LIKE '%" + foods.getOldPrice()+"%'");
			} else {
				sb.append(" AND oldPrice LIKE '%" + foods.getOldPrice()+"%'");
			}
		}
		if(!StringUtils.isEmpty(foods.getPrice())) {
			if (StringUtils.isEmpty(sb.toString())) {
				sb.append("price LIKE '%" + foods.getPrice()+"%'");
			} else {
				sb.append(" AND price LIKE '%" + foods.getPrice()+"%'");
			}
		}
		if(!StringUtils.isEmpty(foods.getDescription())) {
			if (StringUtils.isEmpty(sb.toString())) {
				sb.append("description LIKE '%" + foods.getDescription()+"%'");
			} else {
				sb.append(" AND description LIKE '%" + foods.getDescription()+"%'");
			}
		}
		if(!StringUtils.isEmpty(foods.getName())) {
			if (StringUtils.isEmpty(sb.toString())) {
				sb.append("name LIKE '%" + foods.getName()+"%'");
			} else {
				sb.append(" AND name LIKE '%" + foods.getName()+"%'");
			}
		}
		if(!StringUtils.isEmpty(foods.getImage())) {
			if (StringUtils.isEmpty(sb.toString())) {
				sb.append("image LIKE '%" + foods.getImage()+"%'");
			} else {
				sb.append(" AND image LIKE '%" + foods.getImage()+"%'");
			}
		}
		if(!StringUtils.isEmpty(foods.getRating())) {
			if (StringUtils.isEmpty(sb.toString())) {
				sb.append("rating LIKE '%" + foods.getRating()+"%'");
			} else {
				sb.append(" AND rating LIKE '%" + foods.getRating()+"%'");
			}
		}
		if(!StringUtils.isEmpty(foods.getSellCount())) {
			if (StringUtils.isEmpty(sb.toString())) {
				sb.append("sellCount LIKE '%" + foods.getSellCount()+"%'");
			} else {
				sb.append(" AND sellCount LIKE '%" + foods.getSellCount()+"%'");
			}
		}
		if(!StringUtils.isEmpty(foods.getInfo())) {
			if (StringUtils.isEmpty(sb.toString())) {
				sb.append("info LIKE '%" + foods.getInfo()+"%'");
			} else {
				sb.append(" AND info LIKE '%" + foods.getInfo()+"%'");
			}
		}
		if (StringUtils.isEmpty(sb.toString())) {
			return null;
		} else {
			return sb.toString();
		}
	}

	@Override
	public List<Foods> selectAllByGoodsId(Long goodsId) {
		return foodsDao.selectAllByGoodsId(goodsId);
	}
	
}